﻿using AutoMapper;
using MediatR;
using T_Material.API.Application.Commands.PurchaseCommand;
using T_Material.Domain;
using T_Material.Infrastructure.Interface;

namespace T_Material.API.Application.CommandHandlers.PurchaseHandler
{
    public class PurchaseCreatedCommandHangler : IRequestHandler<PurchaseCreatedCommand, int>
    {
        private readonly IPurchaseRepository purchaseRepository;

        private readonly IMapper mapper;
        public PurchaseCreatedCommandHangler(IPurchaseRepository purchaseRepository, IMapper mapper)
        {
            this.purchaseRepository = purchaseRepository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(PurchaseCreatedCommand request, CancellationToken cancellationToken)
        {
            var info = mapper.Map<Purchase>(request);
            info.Applicant = "李小红";
            info.CreateBy = "张三";
            info.CreateDate = DateTime.Now;
            info.UpdateBy = "";
            info.UpdateDate = null;
            info.IsDel = false;
            info.AuditState = 0;
            var res = await purchaseRepository.Addsync(info);
            return res;
        }
    }
}
